package com.openNetty.common.system;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;

/**
 * @author zhenggm
 * @create 2017-11-20 下午 4:30
 **/


public class LoggerUtil {
    private static final String LOG_CONIFG_FILE_NAME = "/log4j2.xml";

    static {
        init(LOG_CONIFG_FILE_NAME);
    }

    public static void build(String root){
        init(FileUtils.mergePath(root,LOG_CONIFG_FILE_NAME));
    }
    public static void init(String configFilePath){
        InputStream is = null;
        try {
            is = ConfigIOUtil.getInputStream(configFilePath);
            if (is != null){
                ConfigurationSource configurationSource = new ConfigurationSource(
                        is);
                Configurator.initialize(null, configurationSource);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private org.apache.logging.log4j.Logger logger;

    private LoggerUtil(Class<?> clazz) {
        this.logger = LogManager.getLogger(clazz);
    }

    public void info(String message) {
        logger.info(message);
    }

    public void info(String message, Object... params) {
        logger.info(MessageFormat.format(message, params));
    }

    public void warn(String message) {
        logger.warn(message);
    }

    public void warn(String message, Object... params) {
        logger.warn(MessageFormat.format(message, params));
    }

    public void warn(Throwable e) {
        logger.warn(e);
    }

    public void warn(String message, Throwable e) {
        logger.warn(message, e);
    }

    public void warn(String message, Throwable e, Object... params) {
        logger.warn(MessageFormat.format(message, params), e);
    }

    public void error(String message) {
        logger.error(message);
    }

    public void error(String message, Object... params) {
        logger.error(MessageFormat.format(message, params));
    }

    public void error(Throwable e) {
        logger.error(e);
    }

    public void error(String message, Throwable e) {
        logger.error(message, e);
    }

    public void error(String message, Throwable e, Object... params) {
        logger.error(MessageFormat.format(message, params), e);
    }

    public void debug(String message) {
        logger.debug(message);
    }

    public void debug(String message, Object... params) {
        logger.debug(MessageFormat.format(message, params));
    }

    public void debug(Throwable e) {
        logger.debug(e);
    }

    public void debug(String message, Throwable e) {
        logger.debug(message, e);
    }

    public void debug(String message, Throwable e, Object... params) {
        logger.debug(MessageFormat.format(message, params), e);
    }


    public void trace(String message) {
        logger.trace(message);
    }

    public void trace(String message, Object... params) {
        logger.trace(MessageFormat.format(message, params));
    }

    public void trace(Throwable e) {
        logger.trace(e);
    }

    public void trace(String message, Throwable e) {
        logger.trace(message, e);
    }

    public void trace(String message, Throwable e, Object... params) {
        logger.trace(MessageFormat.format(message, params), e);
    }

    public static LoggerUtil getLogger(Class<?> clazz) {
        return new LoggerUtil(clazz);
    }
}
